package uc.org.jkas.app.admin;
import java.util.HashMap;
import java.util.ArrayList;
import uc.org.jkas.model.MembersM;
import org.jkas.utils.Page;
public class Members extends BackBase
{ MembersM msm=new MembersM();
	public void index()
    {  if(IS_POST())
		{
		}
		else
		{int page=Pint("page");
		 int pagenum=20;
		 Page p=new Page(page,pagenum);
		 String keyword=P("keyword");
		 int searchby=Pint("searchby");
         String params="";
		 msm.table("members m").fields("m.*,a.name as appname").join("applications a","m.appid","a.appid");
		  if(!isEmpty(keyword))
			{ switch(searchby)
				{ case 1:
			       msm.where(strc("username like '%",keyword,"%'"));
		           break;
				   case 2:
				    msm.where(strc("mobile like '%",keyword,"%'"));
				   break;
				   case 3:
					msm.where(strc("email like '%",keyword,"%'"));
				   break;
			      default:
                  msm.where(strc("username like '%",keyword,"%'"));
				}
			  
		      params=strc("/searchby/",searchby,"/keyword/",keyword);
			}
		 ArrayList<HashMap> lists=msm.limit(p.getPos(),pagenum).count().order("uid desc").select();
		 p.setCount(msm.getCount());

		  
		 assign("params",params);
		 assign("pageinfo",p.getInfo());
		 assign("lists",lists);
		 display();
		}
	}

	public void add()
	{
	   if(IS_POST())
		{String res=JSON_RESULT_FAILED;
		   HashMap<String,Object> data=new HashMap<String,Object>();
	       String password=P("password");
		   String username=P("username");
           if(!isEmpty(username) && !isEmpty(password))
			{String email=P("email");
		     String mobile=P("mobile");
			     if(!msm.exists(username,mobile,email))
				  {
			        String salt=randStr();
		            password=msm.encodePassword(password,salt);
	                data.put("username",username);
		            data.put("password",password);
		            data.put("salt",salt);
		            data.put("email",P("email"));
		            data.put("mobile",P("mobile"));
		            data.put("regip",ip2int(clientIp()));
		            data.put("regdate",time());
		             if(msm.insert(data)>0)
			           {res=JSON_RESULT_SUCCESS;
			           }
				  }
				  else
				  {res="{\"code\":202,\"msg\":\"exists\"}";
				  }
			}
			else
			{res="{\"code\":401,\"msg\":\"empty username\"}";
			}

          echo(res);
		}
		else
		{ 
			display();
		}
	}

	public void edit()
	{  int uid=Pint("uid");
		if(IS_POST())
		{ String res=JSON_RESULT_FAILED;
		  HashMap<String,Object> data=new HashMap<String,Object>();
	      String password=P("password");
		   String username=P("username");
           if(!isEmpty(username))
			{String email=P("email");
		     String mobile=P("mobile");
			     if(!msm.exists(username,mobile,email,uid))
				  {
                   if(!isEmpty(password))
					  {
			            int salt=random(100000,999999);
		                 password=md5(strc(md5(password),salt));
						 data.put("password",password);
		                 data.put("salt",salt);
					  }
	                data.put("username",username);
		            data.put("email",P("email"));
		            data.put("mobile",P("mobile"));
		             if(msm.update(strc("uid=",uid),data)>0)
			            {res=JSON_RESULT_SUCCESS;
			            }
				  }
				  else
				  {res="{\"code\":202,\"msg\":\"exists\"}";
				  }
			}
			else
			{res="{\"code\":401,\"msg\":\"empty username\"}";
			}
		  echo(res);
		}
		else
		{   
			HashMap<String,String> info=msm.getInfo(uid);
			assign("info",info);
			display();
		}
	}

	public void setstatus()
	 {String res="{\"code\":400,\"msg\":\"failed\"}";
	   
      if (IS_POST()) {
	   int uid = Pint("uid");
	    if(uid>0)
		  {  HashMap<String,Object> data = new HashMap<String,Object>();
	         data.put("status", Pint("status"));
	         if (this.msm.update(strc("uid = ",uid), data) > 0) {
                 res="{\"code\":200,\"msg\":\"ok\"}";
             }
		  }
	  }
	  echo(res);
	 }

	public void drop()
	{ String res=JSON_RESULT_FAILED; 
	 if(IS_POST())
		{
		 int uid=Pint("uid");
	     if(msm.dropInfo(uid)>0)
		 {res=JSON_RESULT_SUCCESS;
		 }
	   }
	   echo(res);
	}
}